package com.htj.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import java.security.SecureRandom;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import net.sf.json.JSONObject;

public class HttpUtil {
   public static JSONObject httpsRequest(String requestUrl, String requestMethod, String outputStr) {
      JSONObject jsonObject = null;

      try {
         TrustManager[] tm = new TrustManager[]{new MyX509TrustManager()};
         SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
         sslContext.init((KeyManager[])null, tm, new SecureRandom());
         SSLSocketFactory ssf = sslContext.getSocketFactory();
         URL url = new URL(requestUrl);
         HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
         conn.setSSLSocketFactory(ssf);
         conn.setDoOutput(true);
         conn.setDoInput(true);
         conn.setUseCaches(false);
         conn.setRequestMethod(requestMethod);
         OutputStream inputStream;
         if (null != outputStr) {
            inputStream = conn.getOutputStream();
            inputStream.write(outputStr.getBytes("UTF-8"));
            inputStream.close();
         }

         InputStream inputstream = conn.getInputStream();
         InputStreamReader inputStreamReader = new InputStreamReader(inputstream, "utf-8");
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
         String str = null;
         StringBuffer buffer = new StringBuffer();

         while((str = bufferedReader.readLine()) != null) {
            buffer.append(str);
         }

         bufferedReader.close();
         inputStreamReader.close();
         inputstream.close();
         inputstream = null;
         conn.disconnect();
         jsonObject = JSONObject.fromObject(buffer.toString());
      } catch (ConnectException var14) {
         System.out.print("连接超时：{}" + var14.getMessage());
      } catch (Exception var15) {
         System.out.print("请求微信 异常------ " + var15.getMessage());
      }

      return jsonObject;
   }
}
